home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games Special 4 / THE BEST OF SELECT Games Special 4 (Select CD-ROM)(1996).iso / win95 / gchess / initiali.c < prev    next >
C/C++ Source or Header  |  1995-02-22  |  14KB  |  491 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  9.   Copyright (c) 1988, 1989, 1990  John Stanback
  10.  
  11.   This file is part of CHESS.
  12.  
  13.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  14.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  15.   the consequences of using it or for whether it serves any particular
  16.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  17.   General Public License for full details.
  18.  
  19.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  20.   only under the conditions described in the CHESS General Public License.
  21.   A copy of this license is supposed to have been given to you along with
  22.   CHESS so you can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice and this
  24.   notice must be preserved on all copies.
  25. */
  26.  
  27. #define NOATOM 
  28. #define NOCLIPBOARD
  29. #define NOCREATESTRUCT
  30. #define NOFONT
  31. #define NOREGION
  32. #define NOSOUND
  33. #define NOWH
  34. #define NOWINOFFSETS
  35. #define NOCOMM
  36. #define NOKANJI
  37.  
  38. #include <windows.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42.  
  43. #ifdef WIN32
  44. #include <winbase.h>
  45. #endif
  46.  
  47. #include "gnuchess.h"
  48. #include "defs.h"
  49.  
  50. #ifdef WIN32
  51. #define _BASEETC
  52. #else
  53. #define _BASEETC _based(_segname("_CODE")) 
  54. #endif
  55.  
  56. extern HWND hStats;
  57.  
  58. /*extern short distdata[64][64], taxidata[64][64];*/
  59. extern short far *distdata, far *taxidata;
  60.  
  61. extern FILE *hashfile;
  62. extern short stage, stage2, Developed[2];
  63. extern short ChkFlag[maxdepth], CptrFlag[maxdepth], PawnThreat[maxdepth];
  64. extern short Pscore[maxdepth], Tscore[maxdepth];
  65. extern short rehash;
  66.  
  67. /* extern struct hashval hashcode[2][7][64]; */
  68. extern struct hashval far *hashcode;
  69. extern unsigned char far * history;
  70.  
  71. void
  72. Initialize_dist (void)
  73. {
  74.   register short a, b, d, di;
  75.  
  76.   for (a = 0; a < 64; a++)
  77.     for (b = 0; b < 64; b++)
  78.       {
  79.         d = abs (column (a) - column (b));
  80.         di = abs (row (a) - row (b));
  81. /*
  82.         taxidata[a][b] = d + di;
  83.         distdata[a][b] = (d > di ? d : di);
  84. */
  85.         *(taxidata+a*64+b) = d + di;
  86.         *(distdata+a*64+b) = (d > di ? d : di);
  87.  
  88.       }
  89. }
  90.  
  91. static short _BASEETC Stboard[64] =
  92. {rook, knight, bishop, queen, king, bishop, knight, rook,
  93.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  94.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  95.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  97.  rook, knight, bishop, queen, king, bishop, knight, rook};
  98.  
  99. static short _BASEETC Stcolor[64] =
  100. {white, white, white, white, white, white, white, white,
  101.  white, white, white, white, white, white, white, white,
  102.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  103.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  104.  black, black, black, black, black, black, black, black,
  105.  black, black, black, black, black, black, black, black};
  106.  
  107. extern short _BASEETC board[64], color[64];
  108.  
  109. /*extern unsigned char nextpos[8][64][64];*/
  110. /*extern unsigned char nextdir[8][64][64];*/
  111. extern unsigned char far * nextpos;
  112. extern unsigned char far * nextdir;
  113. /*
  114.   ptype is used to separate white and black pawns, like this;
  115.   ptyp = ptype[side][piece]
  116.   piece can be used directly in nextpos/nextdir when generating moves
  117.   for pieces that are not black pawns.
  118. */
  119. static short _BASEETC ptype[2][8] =
  120. {
  121.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  122.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  123.  
  124. static short _BASEETC direc[8][8] =
  125. {
  126.   0, 0, 0, 0, 0, 0, 0, 0,
  127.   10, 9, 11, 0, 0, 0, 0, 0,
  128.   8, -8, 12, -12, 19, -19, 21, -21,
  129.   9, 11, -9, -11, 0, 0, 0, 0,
  130.   1, 10, -1, -10, 0, 0, 0, 0,
  131.   1, 10, -1, -10, 9, 11, -9, -11,
  132.   1, 10, -1, -10, 9, 11, -9, -11,
  133.   -10, -9, -11, 0, 0, 0, 0, 0};
  134.  
  135. static short _BASEETC max_steps[8] =
  136. {0, 2, 1, 7, 7, 7, 1, 2};
  137.  
  138. static short _BASEETC nunmap[120] =
  139. {
  140.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  141.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  142.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  143.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  144.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  145.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  146.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  147.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  148.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  149.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  150.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  151.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  152.  
  153.  
  154. void
  155. Initialize_moves (void)
  156.  
  157. /*
  158.   This procedure pre-calculates all moves for every piece from every square.
  159.   This data is stored in nextpos/nextdir and used later in the move generation
  160.   routines.
  161. */
  162.  
  163. {
  164.   short ptyp, po, p0, d, di, s, delta;
  165.   unsigned char far *ppos, far *pdir;
  166.   short dest[8][8];
  167.   short steps[8];
  168.   short sorted[8];
  169.  
  170.   for (ptyp = 0; ptyp < 8; ptyp++)
  171.     for (po = 0; po < 64; po++)
  172.       for (p0 = 0; p0 < 64; p0++)
  173.         {
  174.           *(nextpos+ptyp*64*64+po*64+p0) = (unsigned char) po;
  175.           *(nextdir+ptyp*64*64+po*64+p0) = (unsigned char) po;
  176.         }
  177.   for (ptyp = 1; ptyp < 8; ptyp++)
  178.     for (po = 21; po < 99; po++)
  179.       if (nunmap[po] >= 0)
  180.         {
  181.           ppos = nextpos+ptyp*64*64+nunmap[po]*64;
  182.           pdir = nextdir+ptyp*64*64+nunmap[po]*64;
  183.           /* dest is a function of direction and steps */
  184.           for (d = 0; d < 8; d++)
  185.             {
  186.               dest[d][0] = nunmap[po];
  187.               delta = direc[ptyp][d];
  188.               if (delta != 0)
  189.                 {
  190.                   p0 = po;
  191.                   for (s = 0; s < max_steps[ptyp]; s++)
  192.                     {
  193.                       p0 = p0 + delta;
  194.                       /*
  195.                         break if (off board) or
  196.                         (pawns only move two steps from home square)
  197.                       */
  198.                       if (nunmap[p0] < 0 || (ptyp == pawn || ptyp == bpawn)
  199.                           && s > 0 && (d > 0 || Stboard[nunmap[po]] != pawn))
  200.                         break;
  201.                       else
  202.                         dest[d][s] = nunmap[p0];
  203.                     }
  204.                 }
  205.               else
  206.                 s = 0;
  207.  
  208.               /*
  209.                 sort dest in number of steps order
  210.                 currently no sort is done due to compability with
  211.                 the move generation order in old gnu chess
  212.               */
  213.               steps[d] = s;
  214.               for (di = d; s > 0 && di > 0; di--)
  215.                 if (steps[sorted[di - 1]] == 0) /* should be: < s */
  216.                   sorted[di] = sorted[di - 1];
  217.                 else
  218.                   break;
  219.               sorted[di] = d;
  220.             }
  221.  
  222.           /*
  223.             update nextpos/nextdir,
  224.             pawns have two threads (capture and no capture)
  225.           */
  226.           p0 = nunmap[po];
  227.           if (ptyp == pawn || ptyp == bpawn)
  228.             {
  229.               for (s = 0; s < steps[0]; s++)
  230.                 {
  231.                   ppos[p0] = (unsigned char) dest[0][s];
  232.                   p0 = dest[0][s];
  233.                 }
  234.               p0 = nunmap[po];
  235.               for (d = 1; d < 3; d++)
  236.                 {
  237.                   pdir[p0] = (unsigned char) dest[d][0];
  238.                   p0 = dest[d][0];
  239.                 }
  240.             }
  241.           else
  242.             {
  243.               pdir[p0] = (unsigned char) dest[sorted[0]][0];
  244.               for (d = 0; d < 8; d++)
  245.                 for (s = 0; s < steps[sorted[d]]; s++)
  246.                   {
  247.                     ppos[p0] = (unsigned char) dest[sorted[d]][s];
  248.                     p0 = dest[sorted[d]][s];
  249.                     if (d < 7)
  250.                       pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  251.                     /* else is already initialized */
  252.                   }
  253.             }
  254.         }
  255. }
  256.  
  257. GLOBALHANDLE hGameList, hTree, hHistory, hTTable, hHashCode, hdistdata;
  258. GLOBALHANDLE htaxidata, hnextdir, hnextpos;
  259.  
  260. /* hmm.... shouldn`t main be moved to the interface r